<?php
require_once( "include/incMain.php" );

$message = "";
$show = "";

$action = $_REQUEST['action']; 
$show = $_REQUEST['show']; 
$id = $_REQUEST['id']; 


if($show == "") $show = "add";



function getCharacterID($CharID) {
    $sql = "SELECT character_id FROM objects WHERE id = '$CharID' ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal = $row['character_id'];
    }
    return $retVal;
}
function getValue($id, $table, $field) {
    $sql = "SELECT $field FROM $table WHERE id = '$id' ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal = $row[$field];
    }
    return $retVal;
}
function getSelectCharacter($ObjectID) {
    $retVal = "";
    $array_id = "";
    $array_name = "";
    $sql = "SELECT id, name FROM characters ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $tmpArray[$row['id']] = $row['name'];
    }
    $retVal[] = array( "options" => $tmpArray, "selected" => getCharacterID($ObjectID) );
    return $retVal;
}
function getSelectTypes($ObjectID) {
    $retVal = "";
    $array_id = "";
    $array_name = "";
    $sql = "SELECT id, name FROM objects_types ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $tmpArray[$row['id']] = $row['name'];
    }
    $retVal[] = array( "options" => $tmpArray, "selected" => getValue($ObjectID, "objects", "type") );
    return $retVal;
}
function getSelectStatus($ObjectID) {
    $retVal = "";
    $array_id = "";
    $array_name = "";
    $sql = "SELECT id, name FROM objects_status ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $tmpArray[$row['id']] = $row['name'];
    }
    $retVal[] = array( "options" => $tmpArray, "selected" => getValue($ObjectID, "objects", "status") );
    return $retVal;
}
function getContent($id) {
    $retVal = "";
    if($id=="")  $sql = "SELECT * FROM objects ";
    else $sql = "SELECT * FROM objects WHERE id = '$id' ";

    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        
        // Account
        $account = getValue(getValue($row['character_id'], "characters", "login_id"), "login", "username"); // LOL
        if($account=="") $account = "NPC";
         
        $retVal[] = array( "id" => $row['id'],
                           "name" => $row['name'],
                           "character" => getValue($row['character_id'], "characters", "name"),
                           "account" => $account, 
                           "character_id" => $row['character_id'],
                           "x" => $row['x'],
                           "y" => $row['y'],
                           "type" => getValue($row['type'], "objects_types", "name"),
                           "status" => getValue($row['status'], "objects_status", "name"));
                           
                           
                           
    }
    return $retVal;
}
function addContent() {
    $name = $_REQUEST['objectName'];
    $character = $_REQUEST['objectCharacter'];
    $x = $_REQUEST['objectX'];
    $y = $_REQUEST['objectY'];
    $type = $_REQUEST['objectType'];
    $status = $_REQUEST['objectStatus'];
    
    $sql = "INSERT INTO objects (name, character_id, x, y, type, status) VALUES ('$name', '$character', '$x', '$y', '$type', '$status') ";
    sql($sql);
    return "ADDED";
}
function saveContent() {
    $id = $_REQUEST['id']; 
    $name = $_REQUEST['objectName'];
    $character = $_REQUEST['objectCharacter'];
    $x = $_REQUEST['objectX'];
    $y = $_REQUEST['objectY'];
    $type = $_REQUEST['objectType'];
    $status = $_REQUEST['objectStatus'];
     
    $sql = "UPDATE objects SET name = '$name', character_id = '$character', x = '$x', y = '$y', type = '$type', status = '$status' WHERE id = '$id' ";
    sql($sql);
    return "SAVED";
}
function deleteContent() {
    $id = $_REQUEST['id'];
    sql("DELETE FROM objects WHERE id = '$id' ");
}



if($show == "add") $message = "ADD";
if($show == "edit") $message = "EDIT";
if($show == "delete") $message = "DELETE";

if($action == "add") $message = addContent();
if($action == "save") $message = saveContent();
if($action == "delete") $message = deleteContent();
if($action == "back" || $action == "delete") {
    $id = "";
    $show = "add";
}





$smarty = new mySmarty();
$smarty->assign("Path", glPath );
$smarty->assign("id", $id );
$smarty->assign("action", $action );
$smarty->assign("show", $show );
$smarty->assign("message", $message );
$smarty->assign("menue", getMenue(4) );
$smarty->assign("Content", getContent($id) );
$smarty->assign("ContentSelectCharacter", getSelectCharacter($id) );
$smarty->assign("ContentSelectTypes", getSelectTypes($id) );
$smarty->assign("ContentSelectStatus", getSelectStatus($id) );
$smarty->display(glPath.'/objects.htm');


?>
